package com.kaishengit.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Contact;
import com.kaishengit.entity.ContactNote;
import com.kaishengit.entity.UserProject;
import com.kaishengit.service.ContactNoteService;
import com.kaishengit.service.ContactService;
import com.kaishengit.util.StringUtil;

public class ContactNoteServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method=request.getParameter("m");
		if(StringUtil.isEmpty(method)){
			list(request,response);
		}else if("save".equals(method)){
			save(request,response);
		}
	}


	private void save(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		HttpSession session=request.getSession();
		UserProject userProject=(UserProject)session.getAttribute("userProject");
		String content=request.getParameter("content");
		String id=request.getParameter("id");
		
		ContactNoteService service=new ContactNoteService();
		service.save(content,id,userProject);
		
		List<ContactNote> contactNoteList=service.findById(id);
		request.setAttribute("contactNoteList", contactNoteList);
		ContactService s=new ContactService();
		Contact contact=s.findById(id);
		request.setAttribute("contact", contact);
		List<Contact> contactList=s.findByProjectId(userProject.getProjectId());
		request.setAttribute("contactList", contactList);
		
		request.getRequestDispatcher("/WEB-INF/views/contact.jsp").forward(request, response);
	}

	private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		ContactService service=new ContactService();
		Contact contact=service.findById(id);
		
		
		ContactNoteService s=new ContactNoteService();
		List<ContactNote> contactNoteList=s.findById(id);
		
		request.setAttribute("contact", contact);
		request.setAttribute("contactNoteList", contactNoteList);
		
		request.getRequestDispatcher("/WEB-INF/views/contact_note.jsp").forward(request, response);
	}
	
	
}
